package org.dreamwork.floatingball.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;

import org.dreamwork.floatingball.util.IKeys;
import org.dreamwork.floatingball.R;
import org.dreamwork.floatingball.service.FloatingBallService2;

public class MainActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
    private SharedPreferences spf;
    @Override
    @SuppressWarnings ("all")
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        addPreferencesFromResource (R.xml.pref_layout);

/*
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences (this);
        pref.edit ().putBoolean (IKeys.Keys.KEY_DEBUG, true).apply ();

        setContentView (R.layout.activity_main);
*/
/*
        findViewById (R.id.btn_test).setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                Intent intent = new Intent (MainActivity.this, FloatingBallService2.class);
                startService (intent);
                finish ();
            }
        });
*/
        Intent intent = new Intent (MainActivity.this, FloatingBallService2.class);
        startService (intent);
    }

    @Override
    protected void onResume () {
        super.onResume ();
        spf = getSharedPreferences (IKeys.Keys.KEY_DEBUG, MODE_APPEND);
        spf.registerOnSharedPreferenceChangeListener (this);
    }

    @Override
    protected void onPause () {
        if (spf != null) {
            spf.unregisterOnSharedPreferenceChangeListener (this);
        }
        super.onPause ();
    }

    @Override
    public void onSharedPreferenceChanged (SharedPreferences ref, String key) {
        if (IKeys.Keys.KEY_DEBUG.equals (key)) {
            FloatingBallService2.D = ref.getBoolean (key, false);
        }
    }
}